package org.springframework.transaction.interceptor;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.springframework.transaction.interceptor.TransactionAspectSupportCustom.TransactionInfo;


/**
 * 储存事务类,key为transactionId, value为CommitPoint and TransactionInfo类
 * @author Administrator
 *
 */
public class TransactionFactory {
	private ConcurrentMap<String, CommitPoint> commitPoint;
	private ConcurrentMap<String, TransactionInfo> transactionInfo;
	private static TransactionFactory transactionFactory;
	
	private TransactionFactory() {
		commitPoint = new ConcurrentHashMap<String, CommitPoint>();
		transactionInfo = new ConcurrentHashMap<String, TransactionInfo>();
	}
	
	public static TransactionFactory init() {
		if(transactionFactory == null) {
			transactionFactory = new TransactionFactory();
		}
		return transactionFactory;
	}
	
	public synchronized CommitPoint getCommitPoint(String key) {
		if(key == null || key.isEmpty())
			return null;
		return commitPoint.get(key);
	}

    public synchronized void putCommitPoint(String key, CommitPoint value) {
    	if(key != null && !key.isEmpty()) {
    		commitPoint.put(key, value);
    	}
    }
    
    public synchronized void removeCommitPoint(String key) {
    	if(key != null && !key.isEmpty()) {
    		commitPoint.remove(key);
    	}
    }
    
    public synchronized TransactionInfo getTransactionInfo(String key) {
    	if(key == null || key.isEmpty())
			return null;
		return transactionInfo.get(key);
	}

    public synchronized void putTransactionInfo(String key, TransactionInfo value) {
    	if(key != null && !key.isEmpty()) {
    		transactionInfo.put(key, value);
    	}
    }
    
    public synchronized void removeTransactionInfo(String key) {
    	if(key != null && !key.isEmpty()) {
    		transactionInfo.remove(key);
    	}
	}
}
